package email;

import Base.ResourceTest;
import com.huawei.translate.notification.model.EmailTemplate;
import com.huawei.translate.notification.repository.EmailTemplateRepository;
import com.huawei.translate.notification.service.EmailTemplateType;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.UUID;

@RunWith(SpringRunner.class)
public class when_update_email_template extends ResourceTest {

    @Autowired
    private EmailTemplateRepository emailTemplateRepository;
    private EmailTemplate template;

    @Before
    public void setup() {
        template = new EmailTemplate();
        template.setTemplateName(EmailTemplateType.ApplicationRejected.name());
        template.setTemplate("template");
        template.setId(UUID.randomUUID().toString());
        emailTemplateRepository.save(template);
    }

    @Test
    public void should_get_email_template_by_template_name() {

        ResponseEntity<String> response = this.testRestTemplate.getForEntity("/api/email/" + EmailTemplateType.ApplicationRejected.name(), String.class);
        Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
        Assert.assertEquals(template.getTemplate(), response.getBody());
    }

    @Test
    public void should_update_email_template_by_tempalte_name() {
        String updateEmailTemplate = "update";

        ResponseEntity<String> response = this.testRestTemplate.postForEntity("/api/email/" + EmailTemplateType.ApplicationRejected.name(), updateEmailTemplate, String.class);

        Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
        String updatedEmailTemplate = this.testRestTemplate.getForEntity("/api/email/" + EmailTemplateType.ApplicationRejected.name(), String.class).getBody();
        Assert.assertEquals(updateEmailTemplate, updatedEmailTemplate);
    }

    @After
    public void clean(){
        emailTemplateRepository.deleteAll();
    }
}
